import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { HttpRequest } from '../../../../service/httpRequest';
import { flyIn } from '../../../../animations/fly-in';
import { ModalDirective } from 'ng2-bootstrap/modal';
declare let $:any;
@Component({
  selector: 'app-device-grouppoint',
  templateUrl: './device-grouppoint.component.html',
  styleUrls: ['./device-grouppoint.component.css'],
  animations: [
    flyIn
  ]
})
export class DeviceGrouppointComponent implements OnInit {
  deviceId:string = '';
  groupId:any;
  pointData:any[];
  pointmsg:string = '';
  constructor(private activeRoute:ActivatedRoute,private httpRequest:HttpRequest) { }

  ngOnInit() {
    this.activeRoute.params.subscribe(params=>{
      //console.log(params)
      if(params['deviceId'] == 'none') {
        this.deviceId = '';
        this.groupId = '';
      }
      else{
        this.deviceId = params['deviceId'].split(',')[0];
        this.groupId = params['deviceId'].split(',')[1]
      }     
      
    }); 
    this.getPointdata()  
  }


  getPointdata(){
    this.httpRequest.sendPost('device_property_of_group/page',{
      device_id:this.deviceId.replace('|','/'),
      pid:this.deviceId.split('|')[0],
      group_id:this.groupId
    }).subscribe(res=>{
      //console.log(res)
      if(res.code==700){
        this.pointmsg = res.msg;
      }else{

        this.pointData = res;
      }
     
    });
  }

  comfirmdata(){
      let didstr=Array();
      for(var i=0;i<this.pointData.length;i++){
          if(this.pointData[i].group_show){
              didstr.push(this.pointData[i].property)
          }          
      }

   
        this.httpRequest.sendPost('device_property_of_group/update',{
          device_id:this.deviceId.replace('|','/'),
          pid:this.deviceId.split('|')[0],
          group_id:this.groupId,
          properties:didstr.join(',')
        }).subscribe(res=>{
           //console.log(res)
           if(res.resultStatus==200){
            alert("选择群组参数显示成功！")
            this.getPointdata();
           }   
        });

      }





                

 
}
